package com.mooc.mymooc.controller;

import com.mooc.mymooc.pojo.*;
import com.mooc.mymooc.service.MoocService;
import com.mooc.mymooc.utils.PageModel;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CourseController {

    @Autowired
    private MoocService moocService;

    @RequestMapping(value = "/course-list")
    public String courseList(HttpServletRequest request, String keywords,String sort,Model model,Integer pageIndex){
         if (keywords!=null){
            List<Course> courseList = this.moocService.searchCourse(keywords);
            PageModel pageModel = new PageModel();
            pageModel.setRecordCount(courseList.size());
            pageModel.setPageSize(5);
            if (pageIndex!=null){
                pageModel.setPageIndex(pageIndex);
            }
            model.addAttribute("courseList",courseList);
            model.addAttribute("pageModel",pageModel);
            return "course-list";
        }
        if (sort==null || sort==""){
            sort = "new";
        }
        PageModel pageModel = new PageModel();
        if (pageIndex!=null){
            pageModel.setPageIndex(pageIndex);
        }
        pageModel.setPageSize(12);
        List<Course> courseList = this.moocService.findAllCourse(pageModel,sort);
        model.addAttribute("courseList",courseList);
        model.addAttribute("pageModel",pageModel);
        model.addAttribute("sort",sort);
        return "course-list";
    }

    @RequestMapping(value = "/course-detail")
    public String courseDetail(Model model, HttpSession session,Integer id){
        this.moocService.addClickNums(id);
        Course course = this.moocService.findCourseByIdDetail(id);
        Course course1 = new Course();//获取学生信息
        course1.setId(id);
        Integer[] user_ids = this.moocService.findCourseStudents(course1);
        List<User> students = new ArrayList<User>();
        User user = null;
        for (int i=0;i<user_ids.length;i++){
            user = this.moocService.findById(user_ids[i]);
            students.add(user);
        }
        model.addAttribute("students",students);
        model.addAttribute("course",course);
        UserFavorite userFavorite = new UserFavorite();
        userFavorite.setFav_id(id);
        userFavorite.setFav_type(1);
        UserFavorite userFavorite1 = this.moocService.findIsFavIndex(userFavorite);
        UserFavorite userFavorite2 = new UserFavorite();
        userFavorite2.setFav_id(id);
        userFavorite2.setFav_type(3);
        UserFavorite userFavorite3 = this.moocService.findIsFavIndex(userFavorite2);
        if (userFavorite1!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag","yes");
        }else{
            model.addAttribute("favflag","no");
        }
        if (userFavorite3!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag1","yes");
        }else{
            model.addAttribute("favflag1","no");
        }
        return "course-detail";
    }

    @RequestMapping(value = "/startstudy")
    public String startStudy(@RequestBody TempCourse userCourse, HttpServletRequest request,
                             HttpServletResponse response){
        Integer exists = this.moocService.findUserCourseExits(userCourse);
        JSONObject jsonObject = new JSONObject();
        if (exists>0) {
            jsonObject.put("sta","ok");
            try {
                PrintWriter out = response.getWriter();
                out.print(jsonObject.toString());
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
        Integer rows = this.moocService.insertStartStudy(userCourse);
        this.moocService.updateStudents(userCourse.getCourse_id());
        if (rows>0){
            jsonObject.put("sta","ok");
        }else{
            jsonObject.put("sta","no");
        }
        try {
            PrintWriter out = response.getWriter();
            out.print(jsonObject.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/course-video")
    public String courseVideo(HttpSession session ,Model model,Integer id){
        Course course = this.moocService.findCourseByIdDetail(id);
        model.addAttribute("course",course);
        return "course-video";
    }

    @RequestMapping(value = "/course-comments")
    public String courseComments(Integer id, Model model,HttpServletRequest request){
        Course course = this.moocService.findCourseByIdDetail(id);
        List<CourseComments> courseCommentsList = this.moocService.findAllCommentsByOrgId(id);
        model.addAttribute("comments",courseCommentsList);
        model.addAttribute("course",course);
        return "course-comment";
    }

    @RequestMapping(value = "/add_comments")
    public void addComments(@RequestBody CourseComments courseComments, HttpSession session,
                            HttpServletResponse response) throws Exception{
        User user = (User) session.getAttribute("user_session");
        courseComments.setUser(user);
        Integer rows = this.moocService.addCourseComments(courseComments);
        JSONObject jsonObject = new JSONObject();
        PrintWriter out = response.getWriter();
        if (rows>0){
            jsonObject.put("sta","ok");
            out.print(jsonObject.toString());
        }
    }
}
